//
//  SYLoadingView.m
//  BasicSDKDemo
//
//  Created by huchu on 2018/8/8.
//  Copyright © 2018年 sysjones. All rights reserved.
//

#import "SYLoadingView.h"
#import "YYKit.h"
#import "Masonry.h"
@implementation SYLoadingViewConfig
+(SYLoadingViewConfig*)shared {
    static SYLoadingViewConfig* _shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shared = [[SYLoadingViewConfig alloc] init];
    });
    return _shared;
}
-(instancetype)init{
    if (self = [super init]) {
        self.titleColor =[UIColor whiteColor];
        self.titleFont =[UIFont systemFontOfSize:15];
        self.hudBackgroundViewColor=[UIColor clearColor];
        self.hudContentBackgroundColor =[[UIColor blackColor] colorWithAlphaComponent:0.6];
        self.hudContentAnimatedBackgroundColor=[UIColor clearColor];
        self.hudCornerRadius=6;
        self.hudInset=UIEdgeInsetsMake(10, 10, 10, 10);
        self.hudAnimatedInset=UIEdgeInsetsZero;
        self.animatedImageViewSize =CGSizeMake(120, 120);
    }
    return self;
}
@end
@implementation SYLoadingView {
    LoadingViewType type;
    YYAnimatedImageView *animatedImageView;
    UILabel *titleLabel;
    UIActivityIndicatorView *indicatorView;
    __weak UIView *superView;
    NSString *titleString;
    NSString *imageString;
    UIView*contentView;
}
-(instancetype)initWithType :(LoadingViewType)loadingType title:(NSString *)string imageString:(NSString *)imageString {
    self =[super init];
    if (self) {
        type =loadingType;
        titleString=string;
        self->imageString =imageString;
        [self initSubview];
    }
    return self;
}
-(void)initSubview {
    self.backgroundColor =[SYLoadingViewConfig shared].hudBackgroundViewColor;
    contentView = [UIView new];
    [self addSubview:contentView];
    if (type==LoadingViewType_indicator) {
        contentView.backgroundColor = [SYLoadingViewConfig shared].hudContentBackgroundColor;
        indicatorView=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [contentView addSubview:indicatorView];
        titleLabel =[UILabel new];
        titleLabel.text = titleString;
        titleLabel.numberOfLines=0;
        titleLabel.font=[SYLoadingViewConfig shared].titleFont;
        titleLabel.textAlignment=NSTextAlignmentCenter;
        titleLabel.textColor =[SYLoadingViewConfig shared].titleColor;
        [contentView addSubview:titleLabel];
        contentView.layer.cornerRadius =[SYLoadingViewConfig shared].hudCornerRadius;
    }else{
        contentView.backgroundColor =[SYLoadingViewConfig shared].hudContentAnimatedBackgroundColor;
        animatedImageView = [YYAnimatedImageView new];
        [contentView addSubview:animatedImageView];
        animatedImageView.image =[YYImage imageNamed:imageString];
    }
}
-(void)layout{
    if (self->type==LoadingViewType_indicator) {
        [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo([SYLoadingViewConfig shared].hudInset.top);
            make.size.mas_equalTo(CGSizeMake(60, 60));
            make.centerX.mas_equalTo(@0);
            if (!self->titleString.length) {
                make.bottom.mas_equalTo(-[SYLoadingViewConfig shared].hudInset.bottom);
                make.left.mas_greaterThanOrEqualTo([SYLoadingViewConfig shared].hudInset.left);
                make.right.mas_greaterThanOrEqualTo(-[SYLoadingViewConfig shared].hudInset.right);
            }
        }];
        if (self->titleString.length) {
            [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                if (self->titleString.length) {
                    make.top.mas_equalTo(self->indicatorView.mas_bottom).offset(8);
                }
                make.bottom.mas_equalTo(-[SYLoadingViewConfig shared].hudInset.bottom);
                make.centerX.mas_equalTo(@0);
                make.left.mas_greaterThanOrEqualTo([SYLoadingViewConfig shared].hudInset.left);
                make.right.mas_greaterThanOrEqualTo(-[SYLoadingViewConfig shared].hudInset.right);
            }];
        }
    }else{
        [animatedImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(@0);
            make.size.width.lessThanOrEqualTo(@([SYLoadingViewConfig shared].animatedImageViewSize.width));
            make.size.height.lessThanOrEqualTo(@([SYLoadingViewConfig shared].animatedImageViewSize.height));
            if (self->animatedImageView.image.size.height>0) {
                make.width.mas_equalTo(self->animatedImageView.mas_height).multipliedBy (self->animatedImageView.image.size.width/self->animatedImageView.image.size.height);
            }
        }];
    }
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(@0);
        make.size.mas_equalTo(self->superView.size);
    }];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.width.mas_lessThanOrEqualTo(kScreenWidth*2.f/3.f);
        make.width.mas_greaterThanOrEqualTo(self->contentView.mas_height);
    }];
    [self setNeedsLayout];
    [self layoutIfNeeded];
    animatedImageView.layer.cornerRadius =[SYLoadingViewConfig shared].hudCornerRadius;
    animatedImageView.layer.masksToBounds=YES;
}
-(void)showLoading {
    self.alpha=0;
    [superView addSubview:self];
    [self->indicatorView startAnimating];
    [self layout];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha=1;
    }completion:^(BOOL finished) {
    }];
}
-(void)dimissLoading {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha=0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
+(NSArray *)loadingViewForView:(UIView *)view {
    NSMutableArray*array = @[].mutableCopy;
    for (UIView *subv in view.subviews) {
        if ([subv isKindOfClass:self]){
            [array addObject:subv];
        }
    }
    return array;
}
+(void)showLoadingInView:(UIView*)view  imageNamed:(NSString*)imageString{
    SYLoadingView*loadingView = [[SYLoadingView alloc] initWithType:LoadingViewType_animatedView title:nil imageString:imageString];
    loadingView->superView=view == nil ? [UIApplication sharedApplication].keyWindow : view;
    [loadingView showLoading];
}
+(void)showLoadingInView:(UIView*)view  titleString:(NSString *)string{
    SYLoadingView*loadingView = [[SYLoadingView alloc] initWithType:LoadingViewType_indicator title:string imageString:nil];
    loadingView->superView=view == nil ? [UIApplication sharedApplication].keyWindow : view;
    [loadingView showLoading];
}
+(void)dimissLoadingForView:(UIView *)view {
    if (view==nil) {
        view=[UIApplication sharedApplication].keyWindow;
    }
    [[self loadingViewForView:view] makeObjectsPerformSelector:@selector(dimissLoading)];
}
+(void)dimissLoading{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [[self loadingViewForView:window] makeObjectsPerformSelector:@selector(dimissLoading)];
}
@end
